﻿using NXDO.Data.Extension;
using NXDO.Data.Factory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TDM.DbData.DataEntity;
using TDM.WebBase;

namespace TDM.WebBiz.TestData
{
    public class TestTaskFileHelper
    {
        public static void DeleteFile(List<long> fileIDs)
        {
            foreach (var fileID in fileIDs)
            {
                string basePath = BasePathHelper.GetProjectBasePath();
                TestTaskFile file = Get(fileID);
                var filePath = Path.Combine(basePath, file.StorePath, fileID.ToString() + file.FileExtension);
                try
                {
                    File.Delete(filePath);
                }
                catch (Exception) { }

                file.DBLinq().ToCommand(file).Delete();
            }
        }

        public static TestTaskFile Get(long fileID)
        {
            var sql = $"select * from TestTaskFile where  taskfid = {fileID}";
            var rs = DbHelper.SetCommand(sql).ExecuteEntityScalar<TestTaskFile>();
            return rs;
        }

        public static List<TestTaskFile> GetFiles(long taskid)
        {
            var sql = $"select * from TestTaskFile where  taskid = {taskid}";
            var rs = DbHelper.SetCommand(sql).ExecuteEntity<TestTaskFile>().ToList();
            return rs;
        }

        public static IEnumerable<TestTaskFileImg> QueryTaskImgs(long taskid)
        {
            List<TestTaskFileImg> lst = new List<TestTaskFileImg>();
            var BasePath = BasePathHelper.GetProjectBasePath();
            var BaseUrl = WebConfigHelper.BaseFileUrl;
            var files = TestTaskFileHelper.GetFiles(taskid);
            foreach (var file in files)
            {
                var storePath = file.StorePath.Replace("\\", "/");
                string url = BaseUrl + "/Project/" + storePath + "/" + file.TaskFID.ToString() + file.FileExtension;
                lst.Add(new TestTaskFileImg() { name = file.FileName, url = url, taskfid = file.TaskFID });
            }
            return lst;
        }
    }
    /// <summary>
    /// 返回类
    /// </summary>
    public class TestTaskFileImg
    {
        public long taskfid { get; set; }

        public string name { get; set; }

        public string url { get; set; }

    }
}
